package com.wesley.method;

import java.util.Date;
import java.util.Scanner;

public class DemoTestCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入运算类型(+，-，*，/)：");

        String str = scanner.next();//scanner.nextLine();
        //此处的nextLine()导致后面的nextLine()会获取到空格字符，无法进行输入
        //根因：Scanner.nextLine()，是可以录入带空格的字符串
        //相关网址介绍：https://blog.csdn.net/weixin_45701008/article/details/104050829
        /*
        Scanner.nextLine()是会读取控制台的所有内容字符，包括敲下回车键产生的结束符。
        而其他录入方式在读取到录入的有效内容之前，所有的无效内容会自动跳过（例如空格，Tab键，回车键等产生的无效字符）。
        所以当之前使用过其他录入方式，录入结束后Enter产生的结束符会留在控制台，
        此时使用Scanner.nextLine()就会直接接收这个结束符，导致录入直接结束。
         */

        System.out.println("运算符类型为："+str);

        while(!str.equals("q")){
            switch (str){//str  不能加""
                case "+":
                    System.out.println("请输入两个数据做运算：");
                    if(scanner.hasNextDouble()){
                        double addData1 = scanner.nextDouble();
                        double addData2 = scanner.nextDouble();
                        System.out.println(addition(addData1, addData2));
                    }
                    System.out.println("请输入下一次运算符：");
                    str = scanner.next();
                    break;
                case "-":
                    System.out.println("请输入两个数据做运算：");
                    if(scanner.hasNextDouble()){
                        double addData1 = scanner.nextDouble();
                        double addData2 = scanner.nextDouble();
                        System.out.println(subtraction(addData1, addData2));
                    }
                    System.out.println("请输入下一次运算符：");
                    str = scanner.next();
                    break;
                case "*":
                    System.out.println("请输入两个数据做运算：");
                    if(scanner.hasNextDouble()){
                        double addData1 = scanner.nextDouble();
                        double addData2 = scanner.nextDouble();
                        System.out.println(multiply(addData1, addData2));
                    }
                    System.out.println("请输入下一次运算符：");
                    str = scanner.next();
                    break;
                case "/":
                    System.out.println("请输入两个数据做运算：");
                    if(scanner.hasNextDouble()){
                        double addData1 = scanner.nextDouble();
                        double addData2 = scanner.nextDouble();
                        System.out.println(division(addData1, addData2));
                    }
                    System.out.println("请输入下一次运算符：");
                    str = scanner.next();
                    break;
                default:
                    System.out.println("不是基本运算符,退出请输入q");
                    str = scanner.next();
                    break;
            }
            //一定不要使用 == 检测两个字符串是否相等！
            if((str.equals("+")) || str.equals("-") || str.equals("*") || str.equals("/")){
                System.out.println("运算符类型为："+str);
            }else if(!str.equals("q")){
                System.out.println("运算符类型不符号要求");
            }else{
                System.out.println("退出");
                break;
            }
        }

        scanner.close();
    }

    public static double addition(double... numbers){
        if (numbers.length == 0){
            System.out.println("No argument passed");
            return 0;
        }

        double result = numbers[0];//0.0;

        for (int i = 1; i < numbers.length; i++) {
            result += numbers[i];
        }

        return result;
    }

    public static double subtraction(double... numbers){
        if (numbers.length == 0){
            System.out.println("No argument passed");
            return 0;
        }

        double result = numbers[0];//0.0;

        for (int i = 1; i < numbers.length; i++) {
            result -= numbers[i];
        }

        return result;
    }

    public static double multiply(double... numbers){
        if (numbers.length == 0){
            System.out.println("No argument passed");
            return 0;
        }

        double result = numbers[0];//1.0;

        for (int i = 1; i < numbers.length; i++) {
            result *= numbers[i];
        }

        return result;
    }

    public static double division(double... numbers){
        if (numbers.length == 0){
            System.out.println("No argument passed");
            return 0;
        }

        double result = numbers[0];//1.0;

        for (int i = 1; i < numbers.length; i++) {
            if(numbers[i] == 0){
                System.out.println("除数不能为0");
                return 0;
            }
            result /= numbers[i];
        }

        return result;
    }

}
